package BitCreekLib; /**
 * @author Ivan Litsvinenka & Emanuele Ricci
 * Classe che gestisce le MetaInfo relative a un torrent
 *
 */

import BitCreeekClient.Utils;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;


public class MetaInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * Nome del file
     */
    private final String name;

    /**
     * Lunghezza del file in bytes
     */
    private final long size;

    /**
     * Numero di parti in cui e' suddiviso il file
     */
    private final int numParts;

    /**
     * Data di creazione
     */
    private String creationDate;

    /**
     * Array di Hash per ogni parte del torrent
     */
    public String[] hashes;


    /**
     * IntetAddress del tracker (announce)
     */
    public InetAddress tracker;

    /**
     *  Ports del tracker ( tcp/udp )
     */
    private Ports trackerPorts;

    /**
     * Costruttore di MetaInfo
     *
     * @param name     Nome del file
     * @param size     Grandezza del file in bytes
     * @param numParts Numero delle parti in cui e' suddiviso il file
     * @param tracker  IP del Tracker
     * @param trackerPorts
     */
    public MetaInfo ( String name, long size, int numParts, String tracker, Ports trackerPorts ) {
        super ();
        this.name = name;
        this.size = size;
        this.numParts = numParts;
        this.trackerPorts = trackerPorts;
        try {
            this.tracker = InetAddress.getByName ( tracker );
        } catch ( UnknownHostException e ) {
            e.printStackTrace ();
        }
    }

    public MetaInfo ( String name, long size, int numParts, String tracker) {
        super ();
        this.name = name;
        this.size = size;
        this.numParts = numParts;
        try {
            this.tracker = InetAddress.getByName ( tracker );
        } catch ( UnknownHostException e ) {
            e.printStackTrace ();
        }
    }
    
    public void setTrackerAddress( InetAddress tracker ) {
    	this.tracker = tracker;
    }

    public Ports getTrackerPorts() {
        return trackerPorts;
    }

    public InetAddress getTrackerAddress() {
        return tracker;
    }

    public void setTrackerPorts( Ports trackerPorts) {
        this.trackerPorts = trackerPorts;
    }

    public String getName () {
        return name;
    }

    public long getSize () {
        return size;
    }

    public int getNumParts () {
        return numParts;
    }

    public String getHash ( int i ) {
        return this.hashes[ i ];
    }

    public String toString () {
        return this.name + " ( Size: " + Utils.humanSize( this.size )+" )";
	}
}
